public class UpdateCoworkersController {

    String domain = '%@'+ApexPages.currentPage().getParameters().get('ed');
    String company = ApexPages.currentPage().getParameters().get('c');
    String punctuation = '\\.|\\,';
    String prefixes = '^the|^dept of';
    String suffixes = 'co$|company$|inc$|incorporated$|inc$|ltd$|limited$|llc$|corporation$|corp$|plc$';
    String companysearch = '%' + company.toLowerCase().replaceAll(punctuation,'').replaceAll(prefixes,'').replaceAll(suffixes,'').trim() + '%';
    
    private List<Lead> coleads;
    private List<Contact> cocontacts;
    


    public list<Lead> getCoLeads(){
    	System.debug('Company Search Criteria: '+companysearch);
        coleads = [select ID, Name, Company, Status, Email, OwnerId, Description, Technology_Focus__c, Invalid_Email__c
            from Lead where (Company like :companysearch
            OR Email like :domain)
            And IsConverted = False
            And Id <> :ApexPages.currentPage().getParameters().get('id')];
        return coleads;
    }
    
    public String getcompany(){
    	return company;
    }
    
    public list<Contact> getCoContacts(){
        cocontacts = [select ID, Name, Account.Name, Email, OwnerId, Description, Account.Technology_Focus__c, Invalid_Email__c
            from Contact where (Account.Name like :companysearch
            OR Email like :domain)
            And Id <> :ApexPages.currentPage().getParameters().get('id')];
        return cocontacts;
    }
    
    public PageReference UpdateRecords(){
        try{
        	update coleads;
        	update cocontacts;
        } catch (Exception e){
        	ApexPages.addMessages(e);
        } finally {
        	if(!ApexPages.hasMessages()){
        		ApexPages.Message success = new ApexPages.Message(ApexPages.Severity.INFO,'Successfully updated coworkers');
        		ApexPages.addMessage(success);	
        	}
        	
        }
        
        return null;
    }
    
    static testMethod void basicTest(){
    	// Create a Lead, two coworkers, update some fields, and then verify that it worked.
    	
    	// Create and insert the test data
    	
    	String company='ABCDEFGHIJ';
    	
    	List<Lead> testLeads = new List<Lead>{
    		new Lead(FirstName='Test',LastName='Lead 1',Title='VP HR', LeadSource = 'Website', 
    			Technology_Focus__c = 'Resourcelink', Email='test1@ABCDEFGHIJ.com', Company='ABCDEFGHIJ'), 
    		new Lead(FirstName='Test',LastName='Lead 2',Title='Payroll Director', LeadSource = 'Website', 
    			Technology_Focus__c = 'Resourcelink', Email='test2@'+company+'.com', Company='ABCDEFGHIJ')
    		};
    	insert testLeads;
    		
    	Account testAccount = new Account(Name='ABCDEFGHIJ', Industry='Retail', 
    		Type='Prospect', BillingCity = 'Austin', BillingCountry = 'United Stats');
    	insert testAccount;
    		
    	Contact testContact = new Contact(FirstName = 'Test', LastName='Contact 1', AccountId=testAccount.Id, 
    		Title='Director', Email='test3@ABCDEFGHIJ.com' );
    	insert testContact;
    	
    	//Instantiate the page and the controller
    	PageReference page = new PageReference('/apex/UpdateCoworkers');
    	page.getParameters().put('ed',company+'.com');
    	page.getParameters().put('c',company);
    	page.getParameters().put('id',testLeads[0].Id);
        Test.setCurrentPage(page);
        UpdateCoworkersController controller = new UpdateCoworkersController();
        
        //Verify that the coworkers appear.
        list<Lead> coleads = controller.getCoLeads();
        System.Assert(coleads.size()>0);
        list<Contact> cocontacts = controller.getCoContacts();
        System.Assert(coleads.size()>0);
        
        //Update some fields and save
        coleads[0].Description = '3DdDp6fJ1P0c1Wbgej6B';
        controller.UpdateRecords();
        
        //Verify that the changes were committed
        Lead testLead = [select Description from Lead where Id = :coleads[0].Id];
        System.assertEquals(testLead.Description, '3DdDp6fJ1P0c1Wbgej6B');
        
        
    }

}